Ccay.Module.define("ccay.security.role.list",function(page,$S){
	// igrid对象
	var igridObj;
	
	//授权
	page.rolePermission = function(value, cell) {
		Ccay.Page.register("ccay.security.role.grantOperation2Role","manage/security/role/grantOperation2Role.html");
		Ccay.Common.openPage("ccay.security.grant.rolePermission",
				"ccay.security.role.grantOperation2Role", {
					tab : '#listRoleTab'
				}, function() {
					ccay.security.role.grantOperation2Role.init(value);
				});
	};
	//查看
	page.viewPermission = function(value, cell) {
		Ccay.Page.register("ccay.security.role.roleOperationView","manage/security/role/roleOperationView.html");
		// 注册弹出框
		Ccay.Common.openPage("ccay.security.label.viewPermission","ccay.security.role.roleOperationView", {
			width : 800,
			height : 480,
			buttons : [{
				text : "ccay.common.label.close",
				onClick : function() {
					// 关闭方法
					ccay.security.role.roleOperationView.closeDialog();
				}
			}]
		}, function() {
			// 初始化方法
			ccay.security.role.roleOperationView.init(value);
		});
	};
	
	var imgBtns = [
	    	{text:"ccay.security.grant.rolePermission",click:page.rolePermission, permission:"permissionManage:saveRolePermissions", type:'img', imgtype:'edit',isIi8n:true},
	    	{text:"ccay.security.label.viewPermission",click:page.viewPermission, permission:"permissionManage:findPermissionTree", type:'img', imgtype:'view',isIi8n:true}
	    	//{text:"ccay.security.label.viewGroupsAndUsersByRole",click:page.viewGroupsAndUsersByRole, permission:null, type:'img', imgtype:'edit',isIi8n:true}
	];
	var gridOptions={
		wrap: true,
		editMode: true,
		percentWidth:true,
		storageOp:{
			// 设置igrid缓存数据
			cache: true,
			// 设置缓存key值
			cacheKey: ["name"] 
		},	
		columns:[
			 {columnType:'select', multi:true},
			 {freezeable:true, columnType:'operate', btns:imgBtns,width:"10%"},
		     {field:'name',header: "角色名",align:"left", rule: {required: true},width:"30%"},
		     {field:'description',header: "描述",align:"left", width:"60%"}
		],
		ajax:{
			//url: 'services/manage/security/userManage/list/${pageSize}/${curPage}/${orderBy}'
			url: 'services/manage/security/roleManage/list/page/${pageSize}/${curPage}'
	    },
		operation:{
			search:{
				btn:'#btnSearch',
				panel:'#queryRoleForm'
			},
			del: {
				btn: '#btnDelete',
				undoBtn:'#btnUnDelete'
			},
			insert: {
				btn: '#btnAdd',
				defaultRow:{}
			},
			save: {
				btn: '#btnSave',
				ajax: {
	    			url: "services/manage/security/roleManage/batch"
	    		},
	    		fnBeforeSave:function(params,ajax, grid, optOp){
	    			var result = true;
					Ccay.doPut({
			 			async:false,
			 			url:"services/manage/security/roleManage/validateForBatch",
			 			data : params,
			 			success:function(data){
			 				if(data.NameUniqueFail){
			 					Ccay.UI.showMessage("角色名 {0} 重复！".formatValue(data.NameUniqueFail.join(",")), 'error');
			 					result = false;
			 				} else if(data.RoleReferenced){
			 					//是否真正提交在此处理，并阻止后续提交
			 					result = false;
			 					Ccay.UI.confirm("角色 {0} 已被引用，删除时将同时删除其关联的权限，是否继续删除？".formatValue(data.RoleReferenced.join(",")), function(bool) {
								 	if(bool){
								 		grid.save(ajax, true);
								 	}
								});
			 				}
			 			}			
			 		});
			 		return result;
	    		}
			}
		}
	  }
	
	page.ready=function(){
		$S("#listRoleTab").ccayTab();
		$S("#roleGrid").igrid(gridOptions);	
		igridObj = $S("#roleGrid").manager();
	};
	
	page.closeTab = function(isReload) {
		var listRoleTabLength = $('#listRoleTab').length;
		var roleGridLength = $S('#roleGrid').length;
		if(listRoleTabLength && roleGridLength){
			$('#listRoleTab').ccayTabRemoveItem();
			if (isReload)
				$S("#roleGrid").manager().reload(true,true,true);
		}
	};

});